import argparse

from modules.Dijkstra import dijkstra
from util.chart_phrase_utils import PGraph
from util.file_utils import read_yaml, data_resolve, st_resolve
from util.matrix_utils import degree2link, result_resolve, link2degree

if __name__ == '__main__':

    # 加载自动参数
    print('1--->加载启动参数')
    parser = argparse.ArgumentParser()
    parser.add_argument('--cfg', type=str, default='cfg/run.yaml', help='运行配置文件相对路径')
    opt = parser.parse_args()

    # 加载数据
    yaml_data = read_yaml(opt.cfg)
    nodes, links = data_resolve(yaml_data['node__file_path'], yaml_data['link__file_path'])
    route_color = yaml_data['route_color']
    repulsion = int(yaml_data['repulsion'])
    file_name = yaml_data['file_name']
    graph_title = yaml_data['graph_title']
    source, target = st_resolve(yaml_data['st_file_path'])

   # print(nodes, links)
    print('1--->启动参数加载完毕')


    print("2--->开始计算最优路径")
    print(f'-->起点：{source}，终点：{target}')
    degree_graph = link2degree(nodes, links)


    # degree_graph = {
    #     'A': {'B': 2, 'C': 9},
    #     'B': {'A': 2, 'D': 4, 'E': 8},
    #     'C': {'A': 9, 'E': 10, 'F': 3},
    #     'D': {'B': 4, 'E': 1, 'G': 5},
    #     'E': {'B': 8, 'C': 10, 'D': 1, 'F': 11, 'G': 6, 'H': 12},
    #     'F': {'C': 3, 'E': 11, 'H': 17},
    #     'G': {'D': 5, 'E': 6},
    #     'H': {'E': 12, 'F': 17},
    # }

    shortest_paths = dijkstra(degree_graph, source)
    route_index = result_resolve(target, shortest_paths, links)
    print("2--->计算完毕")

    # 图片绘制
    print("3--->开始绘制图片")
    pGraph = PGraph()
    pGraph.set_nodes(var_nodes=nodes)
    pGraph.set_links(node_links=links)
    pGraph.mark_route_and_render(route_index=route_index, color=route_color, title=graph_title, repulsion=repulsion, name=file_name)
    print(f"3--->图片绘制完毕，已存为{file_name}")